<?php

namespace App\Http\Controllers\Admin;

use App\User;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Models\Users;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;

class UsersController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $num = $request->num ?? 10;
        $numUrl = '&num='.$num;
        $keywords = $request->keywords? '&keywords='.$request->keywords : '';

        // 定义选择查询的条件 默认选择开启状态的用户
        $queryList = [
            'status' => 1
        ];

        $authUrl= $auth= '';
        if($request->auth != ''){
            $authUrl = $request->auth? '&auth='.$request->auth : '';
            $auth = $queryList['auth'] = $request->auth;
        }

        if($request->keywords){
            //加载用户列表模板
            $users = Users::where('username', 'like', '%'.$request->keywords.'%')->where($queryList)->paginate($num);
        }else{
            $users = Users::where($queryList)->paginate($num);
        }

        return view('Admin/Users/index', [
            'users' => $users,
            'num'=>$num,
            'numUrl'=>$numUrl,
            'request'=>$request,
            'keywords'=>$keywords,
            'authUrl'=>$authUrl,
            'auth'=>$auth
        ]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //加载添加用户的模板
        return view('Admin/Users/add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //验证用户信息
        $this->validate($request, [
            'username' => 'required',
            'password' => 'required',
            'surepass' => 'required|same:password',
            'status' => 'required',
            'auth' => 'required'
        ],[
            'username.required'=>'用户名不能为空',
            'password.required'=>'密码不能为空',
            'surepass.required'=>'确认密码不能为空',
            'surepass.same'=>'两次密码不一样',
            'status.required'=>'请选择一个状态',
            'auth.required'=>'请选择用户权限'
        ]);

        $data = $request->except(['_token', 'surepass']);
        $data['password'] = Hash::make($data['password']);  //给密码Hash加密
        $data['created_at'] = $data['updated_at'] = date('Y-m-d H:i:s', time());

        $res = Users::insert($data);

        if($res){
            return redirect('/admin/users')->with('success', '添加成功');
        }else{
            return back();  //返回上一页
        }

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $data['status'] = 0;
        $res = Users::where('id', $id)->update($data);
        if($res){
            return redirect('/admin/users')->with('success', '回收成功');
        }else{
            return redirect('/admin/users')->with('error', '回收失败');
        }
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $data = Users::find($id)->toArray();
        return view('Admin/Users/edit', ['data'=>$data]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        $this->validate($request, [
            'username' => 'required',
            'password' => 'required',
            'surepass' => 'required|same:password',
            'status' => 'required',
            'auth' => 'required'
        ],[
            'username.required'=>'用户名不能为空',
            'password.required'=>'密码不能为空',
            'surepass.required'=>'确认密码不能为空',
            'surepass.same'=>'两次密码不一样',
            'status.required'=>'请选择一个状态',
            'auth.required'=>'请选择用户权限'
        ]);

        $data = $request->except(['_token', 'surepass', '_method']);
        $data['password'] = Hash::make($data['password']);  //给密码Hash加密

        $res = Users::where('id', $id)->update($data);

        if($res){
            return redirect('/admin/users')->with('success', '修改成功');
        }else{
            return back()->with('error', '修改失败');  //返回上一页
        }

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //



    }

    // 回收站页面
    public function trash(Request $request)
    {
        $num = $request->num ?? 10;
        $numUrl = '&num='.$num;
        $keywords = $request->keywords? '&keywords='.$request->keywords : '';

        // 定义选择查询的条件 默认选择开启状态的用户
        $queryList = [
            'status' => 0
        ];

        $authUrl= $auth= '';
        if($request->auth != ''){
            $authUrl = $request->auth? '&auth='.$request->auth : '';
            $auth = $queryList['auth'] = $request->auth;
        }

        if($request->keywords){
            //加载用户列表模板
            $users = Users::where('username', 'like', '%'.$request->keywords.'%')->where($queryList)->paginate($num);
        }else{
            $users = Users::where($queryList)->paginate($num);
        }

        return view('Admin/Users/trash', [
            'users' => $users,
            'num'=>$num,
            'numUrl'=>$numUrl,
            'request'=>$request,
            'keywords'=>$keywords,
            'authUrl'=>$authUrl,
            'auth'=>$auth
        ]);
    }

    // 回收站 恢复状态
    public function recover(Request $request)
    {
        $id = $request->id;
        $data['status'] = 1;
        $res = Users::where('id', $id)->update($data);
        if($res){
            return redirect('/admin/user/trash')->with('success', '恢复成功');
        }else{
            return redirect('/admin/user/trash')->with('error', '恢复失败');
        }
    }

    // 回收站彻底删除
    public function del(Request $request)
    {
        $id = $request->id;
        $res = Users::destroy($id);
        if($res){
            return redirect('/admin/user/trash')->with('success', '删除成功');
        }else{
            return redirect('/admin/user/trash')->with('error', '删除失败');
        }
    }

}
